package com.ithinks.flow.common.utils.protocol.udp.broadcast.acceptside;

import com.ithinks.flow.common.utils.protocol.udp.broadcast.LogMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.CharsetUtil;

import java.util.List;

/**
 * @Description
 * @ClassName LogEventDecoder
 * @Author MR.LI
 * @date 2020.12.13 17:28
 */
public class LogEventDecoder extends MessageToMessageDecoder<DatagramPacket> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket, List<Object> out) throws Exception {
        ByteBuf data = datagramPacket.content();
        long sendTime = data.readLong();
        long msgId = data.readLong();
        byte sepa = data.readByte();
        int idx = data.readerIndex();
        String sendMsg = data.slice(idx,data.readableBytes()).toString(CharsetUtil.UTF_8);
        LogMsg logMsg = new LogMsg(datagramPacket.sender(),msgId,sendMsg);
        out.add(logMsg);
    }
}
